﻿using UnityEngine;
using System.Collections;

public class ProjectileScript : MonoBehaviour
{

	public Transform shotPrefab;
	public bool isUnlocked = true;
	public float shootingRate = 0.25f;

	private float shootCooldown;

	void Start ()
	{
		shootCooldown = 0f;
	}
	
	void Update ()
	{
		if(shootCooldown > 0)
		{
			shootCooldown -= Time.deltaTime;
		}
	}

	public void Attack(bool isEnemy)
	{
		if(CanAttack && isUnlocked)
		{
			shootCooldown = shootingRate;

			Transform shotTransform = Instantiate(shotPrefab) as Transform;

			shotTransform.position = transform.position;

			ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
			if(shot)
			{
				shot.isEnemyShot = isEnemy;
			}

			MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
			if(move)
			{
				move.direction = this.transform.right;
			}
		}
	}

	public bool CanAttack
	{
		get
		{
			return shootCooldown <= 0.0f;
		}
	}
}
